package com.zjw.zy.manager;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.navigation.NavBackStackEntry;

import com.zjw.zy.R;
import com.zjw.zy.base.BaseFragment;
import com.zjw.zy.MainActivity;
import com.zjw.zy.coreui.fragment.memorandum.TextFragment;
import com.zjw.zy.utils.AppUtils;

import java.util.Deque;
import java.util.Iterator;
import java.util.List;

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/9/28 16:17
 */
public final class PageManager {

    private static final String TAG = "PageManager";

    private static Fragment sRoot;
    private static FragmentManager sFragmentManager;


    public static void init(@NonNull MainActivity mainActivity) {
        sRoot = mainActivity.getSupportFragmentManager().findFragmentById(R.id.nav_host_main);
        if (sRoot == null) {
            throw new RuntimeException("sRoot == null");
        }

        sFragmentManager = sRoot.getChildFragmentManager();
    }

    public static BaseFragment getTopFragment() {
        if (sRoot == null) {
            throw new RuntimeException("sRoot == null");
        }
        return (BaseFragment) sFragmentManager.getPrimaryNavigationFragment();
    }

    public static void goMain() {
        singleNavigation(R.id.nav_main_fragment);
    }


    public static void back() {
        AppUtils.getMainActivity().navigationController().navigateUp();
    }

    public static void goMemorandum() {
        singleNavigation(R.id.nav_memorandum_main_fragment);
    }

    public static void goToolsNav() {
        singleNavigation(R.id.nav_tools_nav_fragment);
    }

    public static void goCalendar() {
        singleNavigation(R.id.nav_calendar_fragment);
    }

    public static void goText(@Nullable String text, @Nullable Float textSp) {
        if (getTopFragment() instanceof TextFragment) {
            return;
        }
        Bundle bundle = new Bundle();
        bundle.putString(TextFragment.TEXT_KEY, text == null ? "" : text);
        if (textSp != null) {
            bundle.putFloat(TextFragment.TEXT_SIZE_KEY, textSp);
        } else {
            bundle.putFloat(TextFragment.TEXT_SIZE_KEY, 15f);
        }
        AppUtils.getMainActivity().navigationController().navigate(R.id.nav_text_fragment, bundle);
    }

    @SuppressLint("RestrictedApi")
    public static boolean inStack(@IdRes int id) {
        Deque<NavBackStackEntry> backStack = AppUtils.getMainActivity().navigationController().getBackStack();
        for (NavBackStackEntry navBackStackEntry : backStack) {
            if (navBackStackEntry.getDestination().getId() == id) {
                return true;
            }
        }
        return false;
    }

    public static void singleNavigation(@IdRes int id) {
        if (inStack(id)) {
            AppUtils.getMainActivity().navigationController().popBackStack(id, false);
        } else {
            AppUtils.getMainActivity().navigationController().navigate(id);
        }
    }

    public static void goWechat() {
        singleNavigation(R.id.nav_wechat_fragment);
    }

    public static void goAccess() {
        singleNavigation(R.id.nav_access_fragment);
    }

    @SuppressLint("RestrictedApi")
    @Nullable
    public static NavBackStackEntry getBackStackLastEntry() {
        if (sRoot == null) {
            throw new RuntimeException("sRoot == null");
        }
        Deque<NavBackStackEntry> stack = AppUtils.getMainActivity().navigationController().getBackStack();
        int size = stack.size();
        if (size <= 1) {
            return null;
        }

        Iterator<NavBackStackEntry> iterator = stack.descendingIterator();
        NavBackStackEntry entry = null;
        int count = 0;

        while (iterator.hasNext()) {
            entry = iterator.next();

            if (count < 0) {
                return entry;
            }
            count--;
        }

        return entry;
    }

    public static void startActivity(Class<? extends Activity> target) {
        Intent intent = new Intent(AppUtils.getCurrentActivity(), target);

        AppUtils.getMainActivity().startActivity(intent);
    }
}
